package com.ms.mapper;

import com.ms.pojo.AttendanceRecord;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface AttendanceRecordMapper {
    /**
     * 初始化当天考勤记录
     */
    @Insert("INSERT INTO attendance_record(emp_id, attendance_date, attendance_status, create_time, update_time) " +
            "VALUES(#{empId}, #{attendanceDate}, #{attendanceStatus}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "attendanceId")
    int insert(AttendanceRecord record);

    /**
     * 根据员工ID和日期查询考勤记录
     */
    @Select("SELECT * FROM attendance_record WHERE emp_id = #{empId} AND attendance_date = #{date}")
    AttendanceRecord selectByEmpAndDate(@Param("empId") Integer empId, @Param("date") LocalDate date);

    /**
     * 更新签到时间和状态
     */
    @Update("UPDATE attendance_record SET check_in_time = #{checkInTime}, attendance_status = #{attendanceStatus}, " +
            "update_time = #{updateTime} WHERE attendance_id = #{attendanceId}")
    int updateCheckIn(AttendanceRecord record);

    /**
     * 更新签退时间、状态和早退分钟数
     */
    @Update("UPDATE attendance_record SET check_out_time = #{checkOutTime}, attendance_status = #{attendanceStatus}, " +
            "early_minutes = #{earlyMinutes}, update_time = #{updateTime} WHERE attendance_id = #{attendanceId}")
    int updateCheckOut(AttendanceRecord record);

    /**
     * 查询所有在职员工ID（状态为在职的员工）
     */
    @Select("SELECT emp_id FROM employee WHERE status = 1") // 假设1表示在职状态
    List<Integer> selectAllActiveEmpIds();
     /**
      * 查询所有员工考勤记录
      */
    @Select("SELECT * FROM attendance_record")
    List<AttendanceRecord> selectAllRecords();
}
